﻿@model KL.Store.Web.Models.SatisSiparisModel
@using KL.Store.Core
@{
    Layout = null;
    var pageToPrev = 1;
    var pageToNext = Model.PageCount;
    if (Model.Page > 7)
    {
        pageToPrev = Model.Page - 7;
    }
    if ((Model.Page + 7) < Model.PageCount)
    {
        pageToNext = Model.Page + 7;
    }
    var firstPageClass = "";
    if (Model.Page == 1)
    {
        firstPageClass = " class=\"disabled\"";
    }
    var lastPageClass = "";
    if (Model.Page == Model.PageCount)
    {
        lastPageClass = " class=\"disabled\"";
    }
    var sampleUrl = Model.PageUrl + "?Sort={Sort}&Order={Order}&WithCancelAndComplete={WCAC}&Durum={Durum}&Cari={Cari}&SendMailWhenChangeStatus={SMWCS}&StartDate={StDt}&EndDate={EnDt}";
    var sampleUrlForJs = Model.PageUrl + "?Sort=" + Model.Sort + "&Order=" + Model.Order + "&WithCancelAndComplete={WCAC}&Durum={Durum}&Cari={Cari}&SendMailWhenChangeStatus={SMWCS}&StartDate={StDt}&EndDate={EnDt}";
    var sampleUrlForPagination = "Sort=" + Model.Sort + "&Order=" + Model.Order + "&WithCancelAndComplete=" + Model.WithCancelAndComplete.ToString() + "&Durum=" + Model.DurumId.ToString() + "&Cari=" + Model.CariAdi + "&SendMailWhenChangeStatus=" + Model.SendMailWhenChangeStatus.ToString() + "&StartDate=" + Model.StartDate + "&EndDate=" + Model.EndDate;
    var IdSortUrl = sampleUrl.Replace("{Sort}", "1").Replace("{Order}", ((Model.Sort == 1 && Model.Order == 1) ? "0" : "1")).Replace("{WCAC}", Model.WithCancelAndComplete.ToString()).Replace("{Durum}", Model.DurumId.ToString()).Replace("{Cari}", Model.CariAdi).Replace("{SMWCS}", Model.SendMailWhenChangeStatus.ToString());
    var CariSortUrl = sampleUrl.Replace("{Sort}", "2").Replace("{Order}", ((Model.Sort == 2 && Model.Order == 1) ? "0" : "1")).Replace("{WCAC}", Model.WithCancelAndComplete.ToString()).Replace("{Durum}", Model.DurumId.ToString()).Replace("{Cari}", Model.CariAdi).Replace("{SMWCS}", Model.SendMailWhenChangeStatus.ToString());
    var TarihSortUrl = sampleUrl.Replace("{Sort}", "3").Replace("{Order}", ((Model.Sort == 3 && Model.Order == 1) ? "0" : "1")).Replace("{WCAC}", Model.WithCancelAndComplete.ToString()).Replace("{Durum}", Model.DurumId.ToString()).Replace("{Cari}", Model.CariAdi).Replace("{SMWCS}", Model.SendMailWhenChangeStatus.ToString());
    var OSSortUrl = sampleUrl.Replace("{Sort}", "4").Replace("{Order}", ((Model.Sort == 4 && Model.Order == 1) ? "0" : "1")).Replace("{WCAC}", Model.WithCancelAndComplete.ToString()).Replace("{Durum}", Model.DurumId.ToString()).Replace("{Cari}", Model.CariAdi).Replace("{SMWCS}", Model.SendMailWhenChangeStatus.ToString());
    var DurumSortUrl = sampleUrl.Replace("{Sort}", "5").Replace("{Order}", ((Model.Sort == 5 && Model.Order == 1) ? "0" : "1")).Replace("{WCAC}", Model.WithCancelAndComplete.ToString()).Replace("{Durum}", Model.DurumId.ToString()).Replace("{Cari}", Model.CariAdi).Replace("{SMWCS}", Model.SendMailWhenChangeStatus.ToString());

    var glicon = Model.Order == 1 ? "glyphicon glyphicon-chevron-down" : "glyphicon glyphicon-chevron-up";
}
<ul class="sortandfilter siparissatislistesifiltre">
    <li>
        <label for="CariAdi">Cari İsmi</label>
        @Html.TextBoxFor(m => m.CariAdi, new { @class = "form-control" })
    </li>
    <li>
        <label for="DurumId">Durum</label>
        @Html.DropDownListFor(m => m.DurumId, new SelectList(Model.SiparisDurumlari, "DurumId", "Aciklama"), new { @class = "form-control" })
    </li>
    <li>
        <label for="WithCancelAndComplete">Müşteri Teslim ve İptal Durumları</label>
        <select name="WithCancelAndComplete" id="WithCancelAndComplete" class="form-control">
            <option value="0" @{if (Model.WithCancelAndComplete == 0)
                                {
                <text> selected="selected"</text>}}>Dahil Değil</option>
            <option value="1" @{if (Model.WithCancelAndComplete == 1)
                                {
                <text> selected="selected"</text>}}>Dahil</option>
        </select>
    </li>
    <li>
        <label>Başlangıç Tarihi</label>
        @Html.TextBoxFor(m => m.StartDate, new { @class = "form-control", @id = "datetimepicker1" })
    </li>
    <li>
        <label>Bitiş Tarihi</label>
        @Html.TextBoxFor(m => m.EndDate, new { @class = "form-control", @id = "datetimepicker2" })
    </li>
    <li>
        <button type="button" class="btn btn-primary" onclick=" filtrele(); ">Filtrele</button>
    </li>
    <li>
        <label for="SendMailWhenChangeStatus">Durum değişikliğinde müşteriye</label>
        <select name="SendMailWhenChangeStatus" id="SendMailWhenChangeStatus" class="form-control">
            <option value="1" @{if (Model.SendMailWhenChangeStatus == 1)
                                {
                <text> selected="selected"</text>}}>Mail Gönder</option>
            <option value="0" @{if (Model.SendMailWhenChangeStatus == 0)
                                {
                <text> selected="selected"</text>}}>Mail Gönderme</option>
        </select>
    </li>
</ul>
<div class="clearfix"></div>
<table class="table table-bordered">
    <thead>
        <tr>
            <td><a href="@(IdSortUrl)">Sipariş No @{if (Model.Sort == 1)
                                                    {
                <i class="@(glicon)"></i>
                                                    }}</a></td>
            <td><a href="@(TarihSortUrl)">Tarih @{if (Model.Sort == 3)
                                                  {
                <i class="@(glicon)"></i>
                                                  }}</a></td>
            <td>Cari No</td>
            <td><a href="@(CariSortUrl)">Cari Adı @{if (Model.Sort == 2)
                                                    {
                <i class="@(glicon)"></i>
                                                    }}</a></td>
            <td>Kargo Teslim Tarihi</td>
            <td><a href="@(OSSortUrl)">Ödeme Tipi @{if (Model.Sort == 4)
                                                    {
                <i class="@(glicon)"></i>
                                                    }}</a></td>
            <td>Teslimat Tipi</td>
            <td>Ödeme Tutarı</td>
            <td>Sipariş Tutarı</td>
            <td>Taksit Sayısı</td>
            <td style="width: 245px;"><a href="@(DurumSortUrl)">Durumu @{if (Model.Sort == 5)
                                                                         {
                <i class="@(glicon)"></i>
                                                                         }}</a></td>
        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model.SalesList)
        {
            string TeslimatTipi = "";
            switch (item.TeslimSekli)
            {
                case "D": TeslimatTipi = "Depo Teslim"; break;
                case "K": TeslimatTipi = Parameters.CargoList.Where(c => c.CargoId == item.KargoId).FirstOrDefault() != null ? Parameters.CargoList.Where(c => c.CargoId == item.KargoId).FirstOrDefault().CargoName : "Kargo"; break;
                case "S": TeslimatTipi = "Sevkiyat"; break;
                case "N": TeslimatTipi = "Nakliye"; break;
                default: ; break;
            }
            <tr class="siparisrow" id="siparis@(item.SiparisId)@(item.Tarih.ToString().Replace(".", "").Replace("/", "").Replace("-", "").Replace(":", "").Replace(" ", ""))@(item.GenelToplam.ToString("###,###,##0.00").Replace(".", "").Replace(",", ""))" data-siparisid="@(item.SiparisId)" data-tip="@(item.Tip)" data-posid="@(item.PosId)" data-posname="@(item.PosName)" data-bankid="@(item.BankId)" data-bankname="@(item.BankName)" data-info="@(item.Info)" data-comment="@(item.Comment)">
                <td class="monoclick dblclick"><span class="hoveroverflow">@(item.SiparisId)</span></td>
                <td class="monoclick dblclick"><span class="hoveroverflow">@(item.Tarih)</span></td>
                <td class="monoclick dblclick"><span class="hoveroverflow">@(item.CariId)</span></td>
                <td class="monoclick dblclick"><span class="hoveroverflow">@(item.FaturaAdi)</span></td>
                <td class="monoclick dblclick"><span class="hoveroverflow">@(item.TeslimTar)</span></td>
                <td class="monoclick dblclick"><span class="hoveroverflow">@(Model.OdemeSekilleri.Where(o => o.Kisa == item.OdemeSekli).FirstOrDefault().Uzun)</span></td>
                <td class="monoclick dblclick"><span class="hoveroverflow">@(TeslimatTipi)</span></td>
                <td class="monoclick dblclick"><span class="hoveroverflow">@(item.GenelToplam.ToString("###,###,##0.00 TL"))</span></td>
                <td class="monoclick dblclick"><span class="hoveroverflow">@(item.Payment.ToString("###,###,##0.00 TL"))</span></td>
                <td class="monoclick dblclick"><span class="hoveroverflow">@(item.InstallmentNo)</span></td>
                <td>
                    <div class="">
                        <select class="changestatus" id="item@(item.SiparisId)" data-siparis="@(item.SiparisId)" data-tip="@(item.Tip)" data-original="@(item.DurumId)" data-cari="@(item.CariId)" data-teslim="@(item.TeslimEdildiIsaretlenebilir)">
                            @foreach (var dd in Model.SiparisDurumGuncelle)
                            {
                                <option value="@(dd.DurumId)" @{if (dd.DurumId == item.DurumId)
                                                                {
                                <text>selected="selected"</text>}}>@(dd.Aciklama)</option>
                            }
                        </select>
                        <button class="setnextstatus" id="next@(item.SiparisId)" data-siparis="@(item.SiparisId)" data-tip="@(item.Tip)" data-original="@(item.DurumId)" data-cari="@(item.CariId)" data-teslim="@(item.TeslimEdildiIsaretlenebilir)">Sonraki</button>
                    </div>
                    <span class="result" id="result@(item.SiparisId)"></span>
                </td>
            </tr>
        }
    </tbody>
</table>
<hr />
@if (Model.PageCount > 1)
{
    <ul class="pagination right">
        <li@(firstPageClass)><a href="@(Model.PageUrl)?Page=1&@(sampleUrlForPagination)">&laquo;</a></li>
        @for (var i = pageToPrev; i <= pageToNext; i++)
        {
            <li@{ if (Model.Page == i)
                  {
                <text> class="active"</text>} }><a href="@(Model.PageUrl)?Page=@(i)&@(sampleUrlForPagination)">@(i)</a></li>
        }
        <li@(lastPageClass)><a href="@(Model.PageUrl)?Page=@(Model.PageCount)&@(sampleUrlForPagination)">&raquo;</a></li>
    </ul>
}
<div class="salesPadding"></div>
<div id="saledetails">
    <div id="saledetailscontent" class="col-sm-12 col-md-10 right">
        <div class="col-xs-4 saldetailsbox">
            <div class="form-group">
                <label class="col-sm-2 control-label first">Pos:</label>
                <label id="secposid" class="col-sm-2 control-label first"></label>
                <label id="secposname" class="col-sm-8 control-label first"></label>
            </div>
            <div class="clearfix"></div>
            <div class="form-group">
                <label class="col-sm-2 control-label first">Banka:</label>
                <label id="secbankid" class="col-sm-2 control-label first"></label>
                <label id="secbankname" class="col-sm-8 control-label first"></label>
            </div>
            <div class="clearfix"></div>
            <div class="form-group">
                <label class="col-sm-2 control-label first">Bilgi:</label>
                <label id="secbilgi" class="col-sm-10 control-label first"></label>
            </div>
        </div>
        <div class="col-xs-7 saldetailsbox">
            <div class="form-group">
                <label class="col-sm-2 control-label first">Açıklama:</label>
                <label id="secaciklama" class="col-sm-10 control-label first"></label>
            </div>
        </div>
    </div>
</div>
<div id="resultDiv" style="display: none;"></div>
<div style="display: none;">
    <div id="SiparisDetay">
        <div id="SiparisDetayGet"></div>
    </div>
    <div id="TeslimTarihPH">
        <div class="form-group">
            <label for="teslimtarih">Teslim Tarih Girin</label>
            <input type="text" name="teslimtarih" id="teslimtarih" class="form-control datetimepicker" data-siparis="" />
        </div>
        <button type="button" class="btn btn-primary" onclick="teslimGonder();">Gönder</button>
    </div>
</div>
<script>
    function filtrele()
    {
        var CariIsim = $('#CariAdi').val();
        var DurumId = $('#DurumId').val();
        var WithCancelAndComplete = $('#WithCancelAndComplete').val();
        var SendMailWhenChangeStatus = $('#SendMailWhenChangeStatus').val();
        var StartDate = $('input[name=StartDate]').val();
        var EndDate = $('input[name=EndDate]').val();
        var SampleUrl = '@(sampleUrlForJs)';
        var NewUrl = SampleUrl.replace("{WCAC}", WithCancelAndComplete).replace("{SMWCS}", SendMailWhenChangeStatus).replace("{Durum}", DurumId).replace("{Cari}", CariIsim).replace("{StDt}", StartDate).replace("{EnDt}", EndDate).replace(/&amp;/g, "&");
        window.location.href = NewUrl;
    }

    $('.changestatus').change(function ()
    {
        var sid = $(this).data('siparis');
        var tp = $(this).data('tip');
        var orj = $(this).data('original');
        var teslim = $(this).data('teslim');
        var cari = $(this).data('cari');
        var yeni = $(this).val();
        var mg = $('#SendMailWhenChangeStatus').val();
        var id = $(this).attr('id');

        if (yeni == '10')
        {
            if (confirm('Sipariş iptal edilecektir. Emin misiniz?'))
            {
                durumguncelle(sid, tp, orj, cari, yeni, mg, id);
            }
            else
            {
                $('#item' + sid).val(orj);
            }
        }
        else if (yeni === '6')
        {
            if (teslim === '0')
            {
                if (confirm('Siparişteki ürünler teslim edilmemiş. Emin misiniz?'))
                {
                    teslimTarih(sid, tp, orj, cari);
                }
                else
                {
                    $('#item' + sid).val(orj);
                }
            }
            else
            {
                teslimTarih(sid, tp, orj, cari);
            }
        }
        else
        {
            durumguncelle(sid, tp, orj, cari, yeni, mg, id);
        }
    });
    $('.setnextstatus').click(function ()
    {
        var sid = $(this).data('siparis');
        var tp = $(this).data('tip');
        var orj = $(this).data('original');
        var teslim = $(this).data('teslim');
        var cari = $(this).data('cari');
        var mg = $('#SendMailWhenChangeStatus').val();

        $('.result').html('');
        var nx = $(this).parent().find('select.changestatus option:selected').next().val();
        if (nx == '10')
        {
            if (confirm('Sipariş iptal edilecektir. Emin misiniz?'))
            {
                setnextstatus(sid, tp, mg, cari, orj);
            }
            else
            {
                //alert(orj);
                $('#item' + sid).val(orj);
            }
        }
        else if (nx === '6')
        {
            if (teslim === '0')
            {
                if (confirm('Siparişteki ürünler teslim edilmemiş. Emin misiniz?'))
                {
                    teslimTarih(sid, tp, orj, cari);
                }
                else
                {
                    $('#item' + sid).val(orj);
                }
            }
            else
            {
                teslimTarih(sid, tp, orj, cari);
            }
        }
        else
        {
            setnextstatus(sid, tp, mg, cari, orj);
        }
    });
    $('.monoclick').click(function ()
    {
        $('.table.table-bordered tbody tr').removeClass('selected');
        var get = $(this).parent();
        get.addClass('selected');
        $('#secposid').html(get.data('posid'));
        $('#secposname').html(get.data('posname'));
        $('#secbankid').html(get.data('bankid'));
        $('#secbankname').html(get.data('bankname'));
        $('#secbilgi').html(get.data('info'));
        $('#secaciklama').html(get.data('comment'));
    });
    $('.dblclick').dblclick(function ()
    {
        $('#SiparisDetayGet').html('');
        var get = $(this).parent();
        var siparisId = get.data('siparisid');
        var tip = get.data('tip');
        $('#SiparisDetayGet').load('/ReportPanel/SaleDetailsGet', { SiparisId: siparisId, Tip: tip });

        $.fancybox.open({
            href: '#SiparisDetay',
            autoScale: true,
            width: 1200,
            height: 1000,
            fitToView: true,
            autoSize: false,
            autoDimensions: true,
            type: 'inline',
            afterShow: function ()
            {
            },
            afterClose: function ()
            {
                $('#SiparisDetayGet').html('');
            }
        });
    });


    function durumguncelle(sid, tp, orj, cari, yeni, mg, id)
    {
        $('.result').html('');
        $.ajax({
            url: "/ReportPanel/SalesStatusUpdate",
            data: {
                SiparisId: sid,
                Tip: tp,
                DurumId: yeni,
                MailGonder: mg,
                CariId: cari
            },
            success: function (result)
            {
                if (result == 'Kaydedildi')
                {
                    $('#result' + sid).html('Kaydedildi');
                    //alert('#item' + sid + ' : ' +orj);
                    $('#item' + sid).attr('data-original', yeni);
                    $('#next' + sid).attr('data-original', result);
                }
                else
                {
                    alert(result);
                    $('#item' + sid).val(orj);
                }
            }
        });
    }

    function setnextstatus(sid, tp, mg, cari, orj)
    {
        $.ajax({
            url: "/ReportPanel/SalesStatusNext",
            data: {
                SiparisId: sid,
                Tip: tp,
                MailGonder: mg,
                CariId: cari
            },
            success: function (result)
            {
                if (result > 0)
                {
                    $('#result' + sid).html('Kaydedildi.');
                    $('#item' + sid).val(result);
                    $('#item' + sid).attr('data-original', result);
                    $('#next' + sid).attr('data-original', result);
                }
                else
                {
                    alert('Kaydedilirken bir hata oluştu.');
                    $('#item' + sid).val(orj);
                }
            }
        });
    }

    /*test*/

    function teslimTarih(sid, tp, orj, cari)
    {
        $('#teslimtarih').attr('data-siparis', sid);
        $('#teslimtarih').attr('data-tip', tp);
        $('#teslimtarih').attr('data-original', orj);
        $('#teslimtarih').attr('data-cari', cari);
        var orgz = $('.xdsoft_datetimepicker').css('z-index');

        $.fancybox.open({
            href: '#TeslimTarihPH',
            autoScale: true,
            width: 500,
            height: 200,
            fitToView: true,
            autoSize: false,
            autoDimensions: true,
            type: 'inline',
            openEffect: 'none',
            closeEffect: 'none',
            afterShow: function ()
            {
                $('.xdsoft_datetimepicker').css('z-index', 500000);
            },
            afterClose: function ()
            {
                $('.xdsoft_datetimepicker').css('z-index', orgz);
            }
        });
    }

    function teslimGonder()
    {
        var sid = $('#teslimtarih').data('siparis');
        var tp = $('#teslimtarih').data('tip');
        var orj = $('#teslimtarih').data('original');
        var cari = $('#teslimtarih').data('cari');
        var mg = $('#SendMailWhenChangeStatus').val();
        var yeni = 6;
        var date = $('#teslimtarih').val();
        if (date === '')
        {
            alert('Tarih giriniz');
            return false;
        }
        $.ajax({
            url: "/ReportPanel/SalesStatusUpdateWithDate",
            data: {
                SiparisId: sid,
                Tip: tp,
                MailGonder: mg,
                CariId: cari,
                DurumId: yeni,
                Date: date
            },
            success: function (result)
            {

                if (result === 'Kaydedildi')
                {
                    $('#result' + sid).html('Kaydedildi');
                    $('#item' + sid).val(yeni);
                    $('#item' + sid).attr('data-original', yeni);
                    $('#next' + sid).attr('data-original', result);
                }
                else
                {
                    alert(result);
                    $('#item' + sid).val(orj);
                }
                $('#teslimtarih').val('');
                $.fancybox.close();
            }
        });
    }

</script>
